home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 2000 July / macformat-092.iso / Dreamweaver 3 / Configuration / Commands / Optimize Image in Fireworks.js < prev    next >
Encoding:
Text File  |  1999-12-01  |  4.3 KB  |  138 lines

  1. // Copyright 1998 Macromedia, Inc. All rights reserved.
  2. //
  3. // Command: Optimize Image in Fireworks
  4. //
  5. // This formless command attempts to find and launch 
  6. // a Fireworks optimization session on a selected image
  7. // within Dreamweaver.
  8. //
  9. // Most of the work to do this is implemented in the
  10. // extension DLL FWLaunch.DLL.
  11. //
  12. // **************** Commands API *****************
  13.     
  14.    function canAcceptCommand()
  15.    {
  16.       // First validate that FWLaunch.DLL has been loaded
  17.       //
  18.       if ( typeof( FWLaunch ) == "undefined" )
  19.          return( false );
  20.          
  21.       // Don't bother to check if a valid version of Fireworks is installed, 
  22.       // because we'll alert the user later about where they can download
  23.       // Fireworks for a free trial if they don't have it installed. 
  24.       // return TRUE if an image is selected
  25.       //         
  26.          var selection = dreamweaver.getSelection();
  27.          var node      = dreamweaver.offsetsToNode( selection[0], selection[1] );
  28.          
  29.          return ( node != null                  && 
  30.                   node.nodeType == Node.ELEMENT && 
  31.                   node.tagName  == "IMG" );
  32.    }
  33.    
  34. //---------------    LOCAL FUNCTIONS   ---------------
  35.  
  36.    function optimizeImage()
  37.    {
  38.          // If they don't have Fireworks 2 or greater installed, give them 
  39.       // the link to the Macromedia site so they can get a free trial version.
  40.       if ( !FWLaunch.validateFireworks(2.0) )
  41.       {
  42.           alert( MSG_Err_FireworksNotInstalled );
  43.         return;
  44.       }
  45.       
  46.       // First check to see if we may launch a session; this
  47.       // currently always returns TRUE for windows, but may
  48.       // return FALSE on the Mac if there's already an 
  49.       // optimization session in progress
  50.       //
  51.       if ( !FWLaunch.mayLaunchFireworks() )
  52.       {
  53.          alert( MSG_Err_MayNotLaunch );
  54.          return;
  55.       }
  56.    
  57.       // Make sure the file has been saved to disk first so
  58.       // we know the document path (which the DLL uses, 
  59.       // among other things, to resolve doc-relative urls
  60.       // and determine working directory (on windows)).
  61.       //
  62.       if ( dreamweaver.getDocumentPath( "document" ) == "" ) {
  63.          if (confirm(MSG_Err_FileNotSaved) && dw.canSaveDocument(dreamweaver.getDocumentDOM('document'))) {
  64.            dw.saveDocument(dreamweaver.getDocumentDOM('document'));
  65.          }
  66.          if ( dreamweaver.getDocumentPath( "document" ) == "" )
  67.            return;
  68.          //otherwise file saved, so continue
  69.       }
  70.       // canAcceptCommand() should have validated that the 
  71.       // selection is an image node; go no further if we
  72.       // don't have a valid SRC attribute
  73.       //
  74.       var selection = dreamweaver.getSelection();
  75.       var node      = dreamweaver.offsetsToNode( selection[0], selection[1] );
  76.       var imageSrc  = node.getAttribute( "src" );
  77.       var width     = node.getAttribute( "width" );
  78.       var height    = node.getAttribute( "height" );
  79.  
  80.       if ( !imageSrc )
  81.       {
  82.          alert( MSG_Err_InvalidUsage );
  83.          return;
  84.       }
  85.       
  86.       // Force width and height to invalid state if they're
  87.       // not defined
  88.       //
  89.       if ( !width )
  90.          width = -1;
  91.          
  92.       if ( !height )
  93.          height = -1;
  94.       
  95.       // Fix up site relative URLs here...
  96.       //
  97.       var siteRoot = dreamweaver.getSiteRoot();
  98.       if ( siteRoot )
  99.       {
  100.          if ( imageSrc.indexOf( '/' ) == 0 )
  101.             imageSrc = siteRoot + imageSrc.substring( 1 );
  102.       }
  103.  
  104.       // Now invoke FWLaunch and process the return code
  105.       //
  106.       var rc = FWLaunch.optimizeInFireworks( unescape( dreamweaver.getDocumentPath( "document" ) )
  107.                                            , unescape( imageSrc ) 
  108.                                            , width
  109.                                            , height );
  110.       switch( rc )
  111.       {
  112.          case 0:
  113.             break; // success!
  114.          
  115.          case 1:
  116.             alert( MSG_Err_InvalidUsage );
  117.             break;
  118.             
  119.          case 2:
  120.             alert( MSG_Err_ResponseFile );
  121.             break;
  122.             
  123.          case 3:
  124.             alert( MSG_Err_Dreamweaver );
  125.             break;
  126.          
  127.          case 4:
  128.             alert( MSG_Err_Fireworks );
  129.             break;
  130.             
  131.          default:
  132.             alert( MSG_Err_UnrecognizedRC + rc );
  133.             break;
  134.       }
  135.  
  136.       return;         
  137.    }
  138.